package com.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.http.NameValuePair;
import org.apache.struts2.ServletActionContext;
import org.hibernate.Query;

import com.alibaba.fastjson.JSON;
import com.bean.OrderBelong;
import com.bean.OrderCreate;
import com.bean.OrderSave;
import com.bean.SongBelong;
import com.bean.SongCreate;
import com.bean.SongSave;
import com.bean.User;
import com.google.gson.JsonObject;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.utils.ChgEcod;
import com.utils.HibCommons;
import com.utils.HttpClientService;
import com.utils.HttpUtils;
import com.utils.ReadJSON;
import com.utils.SearchKuGou;
import com.utils.SearchNetease;
import com.utils.SearchQQ;
import com.utils.SearchWonderful;

import net.sf.json.JSONObject;

public class MainAction extends ActionSupport {
	private User user = new User();
	private OrderCreate ordercreate = new OrderCreate();
	private OrderSave ordersave = new OrderSave();
	private List<OrderCreate> ordercreatelist;
	private List<OrderCreate> ordercreatelist2;
	private List<OrderCreate> ordercreatelist3;
	private OrderBelong orderbelong = new OrderBelong();
	private SongBelong songbelong = new SongBelong();
	private List<OrderBelong> orderbelonglist;
	private List<SongBelong> songbelonglist;
	private SongCreate songcreate = new SongCreate();
	private SongSave songsave = new SongSave();
	private List<SongCreate> songcreatelist;

	/* SongCreate分页方法 */
	private int pageNum_sc;// 当前页
	private int totalpage_sc;// 总页数
	private int pageSize_sc = 3;// 每页条目数
	private int Amount_sc;// 总条目数

	public String index() {
		HibCommons.OpenQuery();
		String hql = "FROM OrderCreate  ORDER BY id DESC";

		Query query = HibCommons.getSession().createQuery(hql);
		query.setFirstResult(1);
		query.setMaxResults(2);
		ordercreatelist = query.list();

		Query query2 = HibCommons.getSession().createQuery(hql);
		query2.setFirstResult(3);
		query2.setMaxResults(4);
		ordercreatelist2 = query2.list();

		Query query3 = HibCommons.getSession().createQuery(hql);
		query3.setFirstResult(7);
		query3.setMaxResults(4);
		ordercreatelist3 = query3.list();
		HibCommons.CloseQuery();
		return "success";
	}

	public String masonry() throws Exception {
		/* 接收当前用户名及头像 */
		HttpServletRequest request = ServletActionContext.getRequest();
		String username = ChgEcod.change(request.getParameter("username"));
		String image = request.getParameter("image");

		/* 开启事务 */
		HibCommons.OpenQuery();
		/* 用用户名从ordercreate表查询所有root的歌曲，结果集放入SongCreateList中 */
		String hql_query_ordercreate = "FROM OrderCreate";
		Query query = HibCommons.getSession().createQuery(hql_query_ordercreate);
		// query.setParameter("username", username);
		ordercreatelist = query.list();
		/* 用"用户名"从belongcreate查询所有root的条目，结果集放入BelongCreateList中 */
		String hql_query_orderbelong = "SELECT orderid FROM OrderBelong where username = :username";
		Query query2 = HibCommons.getSession().createQuery(hql_query_orderbelong);
		query2.setParameter("username", username);
		orderbelonglist = query2.list();
		/* 关闭事务 */
		HibCommons.CloseQuery();
		/* 将用户名，头像，两个集合放入session */
		Map map = ActionContext.getContext().getSession();
		user.setUsername(username);
		user.setImage(image);
		map.put("username", user);
		return SUCCESS;
	}

	public String masonrynologin() throws Exception {

		/* 开启事务 */
		HibCommons.OpenQuery();
		/* 用用户名从ordercreate表查询所有root的歌曲，结果集放入SongCreateList中 */
		String hql_query_ordercreate = "FROM OrderCreate";
		Query query = HibCommons.getSession().createQuery(hql_query_ordercreate);
		// query.setParameter("username", username);
		ordercreatelist = query.list();
		/* 关闭事务 */
		HibCommons.CloseQuery();

		return SUCCESS;
	}

	public String store() throws Exception {
		/* 接收username，image，orderid */
		HttpServletRequest request = ServletActionContext.getRequest();
		String username = ChgEcod.change(request.getParameter("username"));
		String image = request.getParameter("image");
		String orderid = request.getParameter("orderid");
		System.out.println("orderid:" + orderid);
		/* 往BelongCreate表添加数据 */
		HibCommons.OpenQuery();
		OrderBelong store = new OrderBelong();
		store.setUsername(username);
		store.setOrderid(orderid);
		HibCommons.getSession().save(store);

		/* 将收藏的歌单条目添加至OrderSave表中 */
		String hql = "FROM OrderCreate oc WHERE oc.id = :orderid";
		Query query = HibCommons.getSession().createQuery(hql);
		query.setParameter("orderid", orderid);
		ordercreatelist = query.list();

		OrderSave o = new OrderSave();
		o.setOrder_title(ordercreatelist.get(0).getOrder_title());
		o.setOrder_info(ordercreatelist.get(0).getOrder_info());
		o.setOrder_saver(username);
		o.setOrder_img(ordercreatelist.get(0).getOrder_img());
		o.setOrder_date(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
		o.setLim(orderid);
		o.setFromuser(ordercreatelist.get(0).getOrder_creater());
		HibCommons.getSession().save(o);

		HibCommons.CloseQuery();
		/* 将username，image放入session */
		Map map = ActionContext.getContext().getSession();
		user.setUsername(username);
		user.setImage(image);
		map.put("username", user);

		return SUCCESS;
	}

	public String storesong() {
		/* 接收username,image,songid,ordertitle */
		HttpServletRequest request = ServletActionContext.getRequest();
		String username = request.getParameter("username");
		String image = request.getParameter("image");
		String songid = request.getParameter("songid");
		String orderId = request.getParameter("orderId");
		String ordertitle = request.getParameter("ordertitle");
		String pageNum_sc = request.getParameter("pageNum_sc");
		/* 往SongCreate表添加数据 */
		HibCommons.OpenQuery();
		SongBelong songstore = new SongBelong();
		songstore.setUsername(username);
		songstore.setSongid(songid);
		HibCommons.getSession().save(songstore);

		/* 将收藏的歌曲条目添加至SongSave表中 */
		String hql = "FROM SongCreate oc WHERE oc.id = :songid";
		Query query = HibCommons.getSession().createQuery(hql);
		query.setParameter("songid", songid);
		songcreatelist = query.list();
		songsave.setTitle(songcreatelist.get(0).getTitle());
		songsave.setSong_saver(username);
		songsave.setB_order(songcreatelist.get(0).getB_order());
		songsave.setImg(songcreatelist.get(0).getImg());
		songsave.setSinger(songcreatelist.get(0).getSinger());
		songsave.setLim("1");
		songsave.setCd(songcreatelist.get(0).getCd());
		HibCommons.getSession().save(songsave);

		HibCommons.CloseQuery();
		/* 将username,image,ordertitle放入session */
		Map map = ActionContext.getContext().getSession();
		user.setUsername(username);
		user.setImage(image);
		ordercreate.setOrder_title(ordertitle);
		ordercreate.setId(orderId);
		map.put("username", user);
		map.put("createorder", ordercreate);

		return SUCCESS;
	}

	public String querymasonryorder() throws Exception {
		/* 须加分页功能 */
		/*
		 * 从瀑布流进入歌单，须传参数：当前用户名username, 用户名头像image, 要进入的歌单orderId,
		 * 要进入的歌单ordertitle
		 */
		/* 接收参数 */
		HttpServletRequest request = ServletActionContext.getRequest();
		String username = ChgEcod.change(request.getParameter("username"));
		String image = request.getParameter("image");
		String orderId = request.getParameter("orderId");
		String ordertitle = ChgEcod.change(request.getParameter("ordertitle"));

		/* 须加分页功能 */
		Amount_sc = getAmount_sc(orderId);
		this.totalpage_sc = Amount_sc % pageSize_sc == 0 ? (Amount_sc / pageSize_sc) : (Amount_sc / pageSize_sc + 1);
		if (pageNum_sc <= 0)
			this.pageNum_sc = 1;
		if (pageNum_sc > totalpage_sc)
			this.pageNum_sc = totalpage_sc;

		/* 查询并返回结果集 */
		HibCommons.OpenQuery();
		String hql = "FROM SongCreate oc WHERE oc.b_order = :orderId";
		Query query = HibCommons.getSession().createQuery(hql);
		query.setParameter("orderId", orderId);
		query.setFirstResult(((pageNum_sc - 1) * pageSize_sc));
		query.setMaxResults(pageSize_sc);
		songcreatelist = query.list();
		/* 用用户名从songbelong查询所有root的条目，结果集放入SongBelongList中 */
		String hql_query_songbelong = "SELECT songid FROM SongBelong where username = :username";
		Query query2 = HibCommons.getSession().createQuery(hql_query_songbelong);
		query2.setParameter("username", username);
		songbelonglist = query2.list();

		HibCommons.CloseQuery();
		/* 将参数放入session中 */
		Map map = ActionContext.getContext().getSession();
		user.setUsername(username);
		user.setImage(image);
		map.put("username", user);
		ordercreate.setOrder_title(ordertitle);
		ordercreate.setId(orderId);
		map.put("createorder", ordercreate);
		return SUCCESS;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public String querymasonryordernologin() throws Exception {
		/*
		 * 从瀑布流进入歌单，须传参数：当前用户名username, 用户名头像image, 要进入的歌单orderId,
		 * 要进入的歌单ordertitle
		 */
		/* 接收参数 */
		HttpServletRequest request = ServletActionContext.getRequest();
		String orderId = request.getParameter("orderId");
		String ordertitle = ChgEcod.change(request.getParameter("ordertitle"));

		/* 须加分页功能 */
		Amount_sc = getAmount_sc(orderId);
		this.totalpage_sc = Amount_sc % pageSize_sc == 0 ? (Amount_sc / pageSize_sc) : (Amount_sc / pageSize_sc + 1);
		if (pageNum_sc <= 0)
			this.pageNum_sc = 1;
		if (pageNum_sc > totalpage_sc)
			this.pageNum_sc = totalpage_sc;

		/* 查询并返回结果集 */
		HibCommons.OpenQuery();
		String hql = "FROM SongCreate oc WHERE oc.b_order = :orderId";
		Query query = HibCommons.getSession().createQuery(hql);
		query.setParameter("orderId", orderId);
		query.setFirstResult(((pageNum_sc - 1) * pageSize_sc));
		query.setMaxResults(pageSize_sc);
		songcreatelist = query.list();

		HibCommons.CloseQuery();
		/* 将参数放入session中 */
		Map map = ActionContext.getContext().getSession();
		ordercreate.setOrder_title(ordertitle);
		ordercreate.setId(orderId);
		map.put("createorder", ordercreate);
		return SUCCESS;
	}

	@SuppressWarnings("unchecked")
	public String search() throws Exception {
		/* 接收用户填入输入框中的字段 */
		HttpServletRequest request = ServletActionContext.getRequest();
		String s = request.getParameter("s");
		String username = ChgEcod.change(request.getParameter("username"));
		String image = request.getParameter("image");

		/*从QQ音乐搜索*/
		SearchQQ.search(s);
		/*从酷狗搜索*/
		SearchKuGou.search(s);
		/*从网易云音乐搜索，因网易云音乐有反爬虫机制，所以放弃*/
//		SearchNetease.search(s);
		songcreatelist = SearchWonderful.search(s);

		Map map = ActionContext.getContext().getSession();
		user.setUsername(username);
		user.setImage(image);
		map.put("username", user);
		
		map.put("qqname", SearchQQ.getName());
		map.put("qqsinger", SearchQQ.getSinger());
		map.put("id", SearchQQ.getId());
		map.put("qqpic", SearchQQ.getPic());
		
		map.put("kugouname", SearchKuGou.getName());
		map.put("kugousinger", SearchKuGou.getSinger());
		map.put("album_id", SearchKuGou.getAlbumID());
		map.put("hash", SearchKuGou.getFileHash());
//		map.put("kugouurl", SearchKuGou.getUrl());
		map.put("kugoupic", SearchKuGou.getPic());
		
		map.put("songcreatelist", songcreatelist);
//		map.put("neteasename", SearchNetease.getName());
//		map.put("neteasesinger", SearchNetease.getSinger());
//		map.put("neteaseurl", SearchNetease.getUrl());
//		map.put("neteasepic", SearchNetease.getPic());

		return SUCCESS;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public OrderCreate getOrdercreate() {
		return ordercreate;
	}

	public void setOrdercreate(OrderCreate ordercreate) {
		this.ordercreate = ordercreate;
	}

	public List<OrderCreate> getOrdercreatelist() {
		return ordercreatelist;
	}

	public void setOrdercreatelist(List<OrderCreate> ordercreatelist) {
		this.ordercreatelist = ordercreatelist;
	}

	public OrderBelong getOrderbelong() {
		return orderbelong;
	}

	public void setOrderbelong(OrderBelong orderbelong) {
		this.orderbelong = orderbelong;
	}

	public List<OrderBelong> getOrderbelonglist() {
		return orderbelonglist;
	}

	public void setOrderbelonglist(List<OrderBelong> orderbelonglist) {
		this.orderbelonglist = orderbelonglist;
	}

	public SongCreate getSongcreate() {
		return songcreate;
	}

	public void setSongcreate(SongCreate songcreate) {
		this.songcreate = songcreate;
	}

	public List<SongCreate> getSongcreatelist() {
		return songcreatelist;
	}

	public void setSongcreatelist(List<SongCreate> songcreatelist) {
		this.songcreatelist = songcreatelist;
	}

	public SongBelong getSongbelong() {
		return songbelong;
	}

	public void setSongbelong(SongBelong songbelong) {
		this.songbelong = songbelong;
	}

	public List<SongBelong> getSongbelonglist() {
		return songbelonglist;
	}

	public void setSongbelonglist(List<SongBelong> songbelonglist) {
		this.songbelonglist = songbelonglist;
	}

	public OrderSave getOrdersave() {
		return ordersave;
	}

	public void setOrdersave(OrderSave ordersave) {
		this.ordersave = ordersave;
	}

	public SongSave getSongsave() {
		return songsave;
	}

	public void setSongsave(SongSave songsave) {
		this.songsave = songsave;
	}

	public List<OrderCreate> getOrdercreatelist2() {
		return ordercreatelist2;
	}

	public void setOrdercreatelist2(List<OrderCreate> ordercreatelist2) {
		this.ordercreatelist2 = ordercreatelist2;
	}

	public List<OrderCreate> getOrdercreatelist3() {
		return ordercreatelist3;
	}

	public void setOrdercreatelist3(List<OrderCreate> ordercreatelist3) {
		this.ordercreatelist3 = ordercreatelist3;
	}

	public int getPageNum_sc() {
		return pageNum_sc;
	}

	public void setPageNum_sc(int pageNum_sc) {
		this.pageNum_sc = pageNum_sc;
	}

	public int getTotalpage_sc() {
		return totalpage_sc;
	}

	public void setTotalpage_sc(int totalpage_sc) {
		this.totalpage_sc = totalpage_sc;
	}

	public int getPageSize_sc() {
		return pageSize_sc;
	}

	public void setPageSize_sc(int pageSize_sc) {
		this.pageSize_sc = pageSize_sc;
	}

	public int getAmount_sc(String orderId) {
		int i = 0;
		HibCommons.OpenQuery();
		String hql = "SELECT count(*) FROM SongCreate oc WHERE oc.b_order = :orderId";
		Query query = HibCommons.getSession().createQuery(hql);
		query.setParameter("orderId", orderId);
		// 2、获取结果（结果为long类型）
		Object obj = query.uniqueResult();
		// 3、转化为long(类型为long，转为int会报错)
		Long long1 = (Long) obj;
		i = long1.intValue();
		HibCommons.CloseQuery();

		System.out.println("i:" + i);
		return i;
	}

	public void setAmount_sc(int amount_sc) {
		Amount_sc = amount_sc;
	}

}
